package org.example.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.example.utils.RabbitMqUtils;

/**
 * @author zfj
 * @date 2022/4/24 -17:17
 * 声明主题
 * 消费者c1
 */
public class ReceiveLogsTopic01 {
	private static final String EXCHANGE_NAME = "topic_logs";

	public static void main(String[] args) throws Exception {
		Channel channel = RabbitMqUtils.getChannel();
		//声明交换机
		channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
		//声明队列
		String queueName="Q1";
		channel.queueDeclare(queueName,false,false,false,null);
		channel.queueBind(queueName, EXCHANGE_NAME, "*.orange.*");
		System.out.println("等待接收消息.....");
		DeliverCallback deliverCallback=(consumerTag, delivery)->{
			String message=new String(delivery.getBody(),"UTF-8");
			System.out.println("接收队列:"+queueName+
					"绑定键:"+delivery.getEnvelope().getRoutingKey()+
					",消息:"+message);
		};
		//接收消息
		channel.basicConsume(queueName,true, deliverCallback, consumerTag -> {
		});

	}
}
